package com.zlp.mall.portal.controller;


import com.zlp.mall.common.api.R;
import com.zlp.mall.common.util.ComConstants;
import com.zlp.mall.common.util.JwtTokenUtil;
import com.zlp.mall.portal.modules.ums.dto.req.MemberAddReq;
import com.zlp.mall.portal.modules.ums.dto.req.MemberLoginReq;
import com.zlp.mall.portal.modules.ums.dto.resp.MemberResp;
import com.zlp.mall.portal.modules.ums.model.Member;
import com.zlp.mall.portal.modules.ums.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-15
 */
@RestController
@RequiredArgsConstructor
@Api(value = "MemberController", tags = "前台用户管理")
@RequestMapping("/member")
public class MemberController {

    private final MemberService memberService;
    private final HttpSession session;
    private final JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @PostMapping(value = "/register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public R<MemberResp> register(@Valid @RequestBody MemberAddReq addAdminReq) {

        return R.success(memberService.register(addAdminReq));
    }

    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping(value = "/login")
    public R login(@Valid MemberLoginReq memberLoginReq) {

        memberService.login(memberLoginReq.getUsername(), memberLoginReq.getPassword());
        String token = jwtTokenUtil.generateTokenByUserName(memberLoginReq.getUsername());
        Map<String,Object> map = new HashMap<>(3);
        map.put("token",token);
        map.put("tokenHead",tokenHead);
        map.put("username",memberLoginReq.getUsername());
        return R.success(map);
    }



}

